package com.xpgk.mvc.ddc.zcdj.ywsl;


import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.oreilly.servlet.multipart.ExceededSizeException;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.maintenance.vehicleInformation.VehicleInformation;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.tools.PathFileKit;

public class VehicleController  extends  BaseController{
	
	
	public  void  index(){
		BigDecimal count = Db.find("select count(ids) count from ddc_vehicle").get(0).getBigDecimal("count");
		long i = 0;
		if(count!=null){
			i=count.longValue();
		}
		String newString = String.format("%04d", (i+1)); 
		String LSH = new  SimpleDateFormat("yyyyMMdd").format(new Date())+"01"+newString;
		setAttr("LSH",LSH );
		setAttr("organids", getCUserOrganIds());
		render("ywsl.html");
	}
	
	
	public  void  getclxx(){
		String cllx=getPara("cllx");
		List<Record> list=Db.find("select *  from ddc_vehicle_information   where  model =  ? ",cllx);
		Record r = null;
		if(list!=null&&list.size()>0){
			 r = list.get(0);
		 }
		renderJson(r);
	}
	
	
	@Before(Tx.class)
	public  void  saveInfo(){	
		
		UploadFile file = getFile("file1", "temp", 2 * 1024 * 1024, "utf-8"); // 最大上传2M的图片
		UploadFile file2 = getFile("file2", "temp", 2 * 1024 * 1024, "utf-8"); // 最大上传2M的图片
		UploadFile file3 = getFile("file3", "temp", 2 * 1024 * 1024, "utf-8"); // 最大上传2M的图片
		//List<UploadFile> files =getFiles("file");
		
		Vehicle  c = getModel(Vehicle.class,"vehicle");
		
		SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		c.set(Vehicle.column_DJRQ, sdf.format(new Date()) );
//		c.set(Vehicle.column_CCDJRQ, sdf.format(new Date()));
//		c.set(Vehicle.column_ZDYZT, 1);
//		c.set(Vehicle.column_orgid, getCUserOrganIds());
//		c.set(Vehicle.column_FZJG,Organization.dao.findById(getCUserOrganIds()).get(Organization.column_name));
		List<Record> list=Db.find("select *  from ddc_vehicle_information   where  model = ? ",c.get("CLLX"));
		
		if(list==null||(list!=null&&list.size()==0)){
			VehicleInformation v = new VehicleInformation();
			v.set("type", c.get("CLLX"));
			v.set("model", c.get("CLXH"));
			v.set("pp", c.get("CLPP1"));
			v.set("manufacturer", c.get("ZZCMC"));
			v.set("color", c.get("CSYS"));
			v.set("imports", c.get("GCJK"));
			v.set("manu_contry", c.get("ZZG"));
			v.set("chang", c.get("CWKC"));
			v.set("width", c.get("CWKK"));
			v.set("height", c.get("CWKG"));
			v.set("weight", c.get("ZZL"));
			v.set("gl", c.get("GL"));
			v.set("dataStatus", c.get("1"));
			v.save();
		}
		Boolean b = c.save();
		RenderBean result = new RenderBean();
		if(b){
			String IDS = c.get("IDS");
			if(file!=null){
				result = uploadPhoto(file,IDS,"1");
			}
			if(file2!=null){
				result = uploadPhoto(file2,IDS,"2");
			}
			if(file3!=null){
				result = uploadPhoto(file3,IDS,"3");
			}
			
			if(result.getCode() == "1"){
				result.setCode(ConstantRender.render_success_code);
				result.setMsg("保存成功!");
			}else{
				result.setCode(ConstantRender.render_error_code);
				result.setMsg(result.getMsg());
			}
		}else{
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败!");
		}
		renderJson(result);
	}
	

//	@Before(Tx.class)
	public RenderBean uploadPhoto(UploadFile file,String IDS,String lx) {
		RenderBean result = new RenderBean();
		try {
//			UploadFile file = getFile("file", "temp", 2 * 1024 * 1024, "utf-8"); // 最大上传2M的图片
			String fileName = file.getFileName();
			File uploadFile = file.getFile();
			String extension = fileName.substring(fileName.lastIndexOf("."));
			if (".png".equals(extension) || ".jpg".equals(extension) || ".gif".equals(extension) || "jpeg".equals(extension) || "bmp".equals(extension)) {
				// 使用缩略图
//					fileName = new ImageKit().thumbnailImage(uploadFile, 150, 150, "thumb_", false);
//					File source = new File(fileName);
				
//				String savePath = PathKit.getWebRootPath() + "/carPhoto/"+IDS+"_"+CLXH;
				String savePath = "D:/ddc/carPhoto/"+IDS;
				String saveFileName = IDS+"_"+lx +  extension;


				try {
					File p = new File(savePath);
					if (!p.exists())
						p.mkdirs();
						PathFileKit.copyFile(uploadFile, new File(savePath + "/" + saveFileName));
						result.setCode("1");
				} catch (IOException e1) {
					e1.printStackTrace();
					result.setCode("0");
					result.setMsg("文件写入服务器出现错误，请稍后再上传");
				}
//					source.delete();
				//String src = "carPhoto/" +IDS+"/" + saveFileName;
				//result.setCode("src", src); // 相对地址，显示图片用
//					int userID = UserService.getUser(userName).getInt("id");
				//Db.update("update DDC_VEHICLE set PHOTO=? where IDS=?", src, IDS);
			} else {
				result.setCode("0");
				result.setMsg("只允许上传png,jpg,jpeg,gif,bmp类型的图片文件");
			}
//			uploadFile.deleteOnExit();
			uploadFile.delete();
		} catch (Exception e) {
			if (e instanceof ExceededSizeException){
				result.setCode("0");
				result.setMsg("最大上传2M的图片");
			}else{
				result.setCode("0");
				result.setMsg(e.toString());
			}
		}
		return result;
	}
	
}
